package csm.common;

/**
 * @(#)Sprintf.java  ver 1.3  2005/01/22
 *
 *    This is a replacement of printf, the formatted output function of C language.
 *
 *    Example:
 *       Vector vect = new Vector();   // or ArrayList list = new ArrayList();
 *       vect.add("Java");
 *       vect.add(new Double(Math.E));
 *       String str;
 *       str = format("Hello, %10s!  e = %f", vect);        // %10s is a right alignment
 *       System.out.println(str);
 *
 *       str = format("Hello, %-10s!  e = %9.5g", vect);   // %-10s is a left alignment
 *       System.out.println(str);
 *
 *       double[] values = { 15.2035, 2412.0091, 3.82514, 7.2 , -23.569, 0.36245 };
 *       for (int i = 0; i < values.length; i++) {
 *           System.out.println(Sprintf.format("%7.2g", values[i]));   // 7 is the full width; 2 is the width of decimal part.
 *       }
 *
 * @resource http://www.javaclue.org/pub/java/printf/
 * @date     2003/05/14  (ver 1.0)
 * @date     2003/05/30  (ver 1.1)
 * @date     2003/06/19  (ver 1.2)
 * @date     2003/06/19  (ver 1.3)
 * @author   Pilho Kim  (phkim AT cluecom DOT co DOT kr)
 */

import java.text.*;
import java.util.*;

public class Sprintf
{
    public static String spaces(int count)
    {
        StringBuffer sb = new StringBuffer(count);
        for (int i = 0; i < count; i++)
        {
            sb.append(' ');
        }
        return sb.toString();
    }

    public static String sharps(int count)
    {
        StringBuffer sb = new StringBuffer(count);
        for (int i = 0; i < count; i++)
        {
            sb.append('#');
        }
        return sb.toString();
    }

    public static String zeros(int count)
    {
        StringBuffer sb = new StringBuffer(count);
        for (int i = 0; i < count; i++)
        {
            sb.append('0');
        }
        return sb.toString();
    }

    public static int byteLength(String s)
    {
        if( StringUtil.isNullOrEmpty(s)) return 0;
        return s.getBytes().length;
    }

    public static String format(String fmt, boolean b)
    {
        return format(fmt, StringUtil.EMPTY + b);
    }

    public static String format(String fmt, boolean b, String t, String f)
    {
        return b ? format(fmt, t) : format(fmt, f);
    }

    public static String format(String fmt, Object o)
    {
        return format(fmt, o.toString());
    }

    @SuppressWarnings("unused")
    public static String format(String fmt, String s)
    {
        StringBuffer sb = new StringBuffer();
        char[] chars = new char[fmt.length()];

        fmt.getChars(0, fmt.length(), chars, 0);

        final int NORMAL_STATE = 0;
        final int PERCENT_STATE = 1;
        final int L_STATE = 2;
        final int WIDTH_DIGIT_STATE = 3;
        final int POINT_DIGIT_STATE = 4;
        int state = NORMAL_STATE;
        int width = 0;
        int pwidth = 0;
        int blen = 0;
        boolean alignRight = true;
        int pos = 0;
        DecimalFormat df = null;
        Object obj = null;
        String str = "";

        char a;
        for (int i = 0; i < chars.length; i++)
        {
            a = chars[i];
            switch (state)
            {
                case NORMAL_STATE:
                    if (a != '%')
                        sb.append(a);
                    else
                    {
                        width = 0;
                        pwidth = 0;
                        alignRight = true;
                        state = PERCENT_STATE;
                    }
                    break;
                case PERCENT_STATE:
                    if (a == '%')
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    else if (a == '+')
                    {
                        alignRight = true;
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a == '-')
                    {
                        alignRight = false;
                        width = 0;
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a >= '0' && a <= '9')
                    {
                        alignRight = true;
                        width = (int) (a - '0');
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a == '.')
                    {
                        alignRight = true;
                        width = 0;
                        pwidth = 0;
                        state = POINT_DIGIT_STATE;
                    }
                    else if (a == 's' || a == 'S')
                    {
                        str = "" + s;
                        blen = byteLength(str); //
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case WIDTH_DIGIT_STATE:
                    if (a == '.')
                    {
                        pwidth = 0;
                        state = POINT_DIGIT_STATE;
                    }
                    else if (a >= '0' && a <= '9')
                    {
                        width = 10 * width + (int) (a - '0');
                    }
                    else if (a == 's' || a == 'S')
                    {
                        str = "" + s;
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case POINT_DIGIT_STATE:
                    if (a >= '0' && a <= '9')
                    {
                        pwidth = 10 * pwidth + (int) (a - '0');
                    }
                    else if (a == 's' || a == 'S')
                    {
                        str = "" + s;
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
            }
        }
        return sb.toString();
    }

    public static String format(String fmt, char c)
    {
        return format(fmt, (long) (c & 0xFFFF));
    }

    public static String format(String fmt, byte b)
    {
        return format(fmt, (long) b);
    }

    public static String format(String fmt, short n)
    {
        return format(fmt, (long) n);
    }

    public static String format(String fmt, int n)
    {
        return format(fmt, (long) n);
    }

    @SuppressWarnings("unused")
    public static String format(String fmt, long n)
    {
        StringBuffer sb = new StringBuffer();
        char[] chars = new char[fmt.length()];
        fmt.getChars(0, fmt.length(), chars, 0);
        final int NORMAL_STATE = 0;
        final int PERCENT_STATE = 1;
        final int L_STATE = 2;
        final int WIDTH_DIGIT_STATE = 3;
        final int POINT_DIGIT_STATE = 4;
        int state = NORMAL_STATE;
        int width = 0;
        int pwidth = 0;
        int blen = 0;
        boolean alignRight = true;
        int pos = 0;
        DecimalFormat df = null;
        Object obj = null;
        String str = "";
        boolean leadZero = false;
        char a;
        for (int i = 0; i < chars.length; i++)
        {
            a = chars[i];
            switch (state)
            {
                case NORMAL_STATE:
                    if (a != '%')
                        sb.append(a);
                    else
                    {
                        width = 0;
                        pwidth = 0;
                        alignRight = true;
                        leadZero = false;
                        state = PERCENT_STATE;
                    }
                    break;
                case PERCENT_STATE:
                    if (a == '%')
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    else if (a == '+')
                    {
                        alignRight = true;
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a == '-')
                    {
                        alignRight = false;
                        width = 0;
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a >= '0' && a <= '9')
                    {
                        if (a == '0')
                            leadZero = true;
                        alignRight = true;
                        width = (int) (a - '0');
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a == '.')
                    {
                        alignRight = true;
                        width = 0;
                        pwidth = 0;
                        state = POINT_DIGIT_STATE;
                    }
                    else if (a == 'c' || a == 'C')
                    {
                        str = "" + (char) n;
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'x' || a == 'X')
                    {
                        str = "" + n;
                        if (a == 'X')
                            str = str.toUpperCase();
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'l' || a == 'L')
                    {
                        state = L_STATE;
                    }
                    else if (a == 'o' || a == 'O')
                    {
                        str = "" + Long.toOctalString(n);
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'd' || a == 'D')
                    {
                        str = "" + n;
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case L_STATE:
                    if (a == 'x' || a == 'X')
                    {
                        str = "" + Long.toHexString(n);
                        if (a == 'X')
                            str = str.toUpperCase();
                        blen = byteLength(str); //
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'd' || a == 'D' || a == 'u' || a == 'U')
                    {
                        str = "" + n;
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case WIDTH_DIGIT_STATE:
                    if (a == '.')
                    {
                        pwidth = 0;
                        state = POINT_DIGIT_STATE;
                    }
                    else if (a >= '0' && a <= '9')
                    {
                        if (a == '0' && width == 0)
                            leadZero = true;
                        width = 10 * width + (int) (a - '0');
                    }
                    else if (a == 'c' || a == 'C')
                    {
                        str = "" + (char) (n & 0xFFFFL);
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                        {
                            sb.append(str);
                        }
                        state = NORMAL_STATE;
                    }
                    else if (a == 'x' || a == 'X')
                    {
                        str = "" + Long.toHexString(n);
                        if (a == 'X')
                            str = str.toUpperCase();
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'o' || a == 'O')
                    {
                        str = "" + Long.toOctalString(n);
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'l' || a == 'L')
                    {
                        state = L_STATE;
                    }
                    else if (a == 'd' || a == 'D' || a == 'u' || a == 'U')
                    {
                        str = "" + n;
                        blen = byteLength(str);
                        if (width > blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case POINT_DIGIT_STATE:
                    if (a >= '0' && a <= '9')
                    {
                        pwidth = 10 * pwidth + (int) (a - '0');
                    }
                    else if (a == 'c' || a == 'C')
                    {
                        str = "" + (char) (n & 0xFFFFL);
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        state = NORMAL_STATE;
                    }
                    else if (a == 'l' || a == 'L')
                    {
                        state = L_STATE;
                    }
                    else if (a == 'd' || a == 'D' || a == 'u' || a == 'U')
                    {
                        str = "" + n;
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
            }
        }
        return sb.toString();
    }

    public static String format(String fmt, float n)
    {
        return format(fmt, (double) n);
    }

    @SuppressWarnings("unused")
    public static String format(String fmt, double d)
    {
        StringBuffer sb = new StringBuffer();
        char[] chars = new char[fmt.length()];
        fmt.getChars(0, fmt.length(), chars, 0);
        final int NORMAL_STATE = 0;
        final int PERCENT_STATE = 1;
        final int L_STATE = 2;
        final int WIDTH_DIGIT_STATE = 3;
        final int POINT_DIGIT_STATE = 4;
        int state = NORMAL_STATE;
        int width = 0;
        int pwidth = 0;
        int blen = 0;
        boolean alignRight = true;
        int pos = 0;
        DecimalFormat df = null;
        Object obj = null;
        String str = "";
        char a;
        for (int i = 0; i < chars.length; i++)
        {
            a = chars[i];
            switch (state)
            {
                case NORMAL_STATE:
                    if (a != '%')
                        sb.append(a);
                    else
                    {
                        width = 0;
                        pwidth = 0;
                        alignRight = true;
                        state = PERCENT_STATE;
                    }
                    break;
                case PERCENT_STATE:
                    if (a == '%')
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    else if (a == '+')
                    {
                        alignRight = true;
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a == '-')
                    {
                        alignRight = false;
                        width = 0;
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a >= '0' && a <= '9')
                    {
                        alignRight = true;
                        width = (int) (a - '0');
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a == '.')
                    {
                        alignRight = true;
                        width = 0;
                        pwidth = 0;
                        state = POINT_DIGIT_STATE;
                    }
                    else if (a == 'x' || a == 'X')
                    {
                        str = "" + Long.toHexString(Double.doubleToLongBits(d));
                        if (a == 'X')
                            str = str.toUpperCase();
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'l' || a == 'L')
                    {
                        state = L_STATE;
                    }
                    else if (a == 'o' || a == 'O')
                    {
                        str = ""
                                + Long
                                        .toOctalString(Double
                                                .doubleToLongBits(d));
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'e' || a == 'E')
                    {
                        df = new DecimalFormat("##0.###E0");
                        str = df.format(d);
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'f' || a == 'g' || a == 'F' || a == 'G')
                    {
                        str = "" + d;
                        blen = byteLength(str); //
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case L_STATE:
                    if (a == 'x' || a == 'X')
                    {
                        str = "" + Long.toHexString(Double.doubleToLongBits(d));
                        if (a == 'X')
                            str = str.toUpperCase();
                        if (width > str.length())
                        {
                            if (alignRight)
                                sb.append(spaces(width - str.length()) + str);
                            else
                                sb.append(str + spaces(width - str.length()));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'd' || a == 'u' || a == 'D' || a == 'U')
                    {
                        str = "" + (long) Math.floor(d);
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case WIDTH_DIGIT_STATE:
                    if (a == '.')
                    {
                        pwidth = 0;
                        state = POINT_DIGIT_STATE;
                    }
                    else if (a >= '0' && a <= '9')
                    {
                        width = 10 * width + (int) (a - '0');
                    }
                    else if (a == 'x' || a == 'X')
                    {
                        str = "" + Long.toHexString(Double.doubleToLongBits(d));
                        if (a == 'X')
                            str = str.toUpperCase();
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'o' || a == 'O')
                    {
                        str = ""
                                + Long
                                        .toOctalString(Double
                                                .doubleToLongBits(d));
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'e' || a == 'E')
                    {
                        df = new DecimalFormat("##0.###E0");
                        str = df.format(d);
                        blen = byteLength(str);
                        if (width > blen)
                        {
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'l' || a == 'L')
                    {
                        state = L_STATE;
                    }
                    else if (a == 'f' || a == 'g' || a == 'F' || a == 'G')
                    {
                        str = "" + d;
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case POINT_DIGIT_STATE:
                    if (a >= '0' && a <= '9')
                    {
                        pwidth = 10 * pwidth + (int) (a - '0');
                    }
                    else if (a == 'l' || a == 'L')
                    {
                        state = L_STATE;
                    }
                    else if (a == 'e' || a == 'E')
                    {
                        df = new DecimalFormat("##0.###E0");
                        str = df.format(d);
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'f' || a == 'g' || a == 'F' || a == 'G')
                    {
                        if (pwidth > 0)
                        {
                            df = new DecimalFormat("0." + zeros(pwidth));
                            str = df.format(d);
                            blen = byteLength(str);
                            if (width >= blen)
                            { // Fix a bug of 1.1
                                if (alignRight)
                                    sb.append(spaces(width - blen) + str);
                                else
                                    sb.append(str + spaces(width - blen));
                            }
                        }
                        else
                        {
                            str = "" + d;
                            blen = byteLength(str);
                            if (width >= blen)
                            { //
                                if (alignRight)
                                    sb.append(spaces(width - blen) + str);
                                else
                                    sb.append(str + spaces(width - blen));
                            }
                        }
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
            }
        }
        return sb.toString();
    }

    @SuppressWarnings("rawtypes")
    public static String format(String fmt, Vector v)
    {
        StringBuffer sb = new StringBuffer();
        char[] chars = new char[fmt.length()];
        fmt.getChars(0, fmt.length(), chars, 0);
        final int NORMAL_STATE = 0;
        final int PERCENT_STATE = 1;
        final int L_STATE = 2;
        final int WIDTH_DIGIT_STATE = 3;
        final int POINT_DIGIT_STATE = 4;
        int state = NORMAL_STATE;
        int width = 0;
        int pwidth = 0;
        int blen = 0;
        boolean alignRight = true;
        int pos = 0;
        DecimalFormat df = null;
        Object obj = null;
        String str = "";
        boolean leadZero = false;
        char a;
        for (int i = 0; i < chars.length; i++)
        {
            a = chars[i];
            switch (state)
            {
                case NORMAL_STATE:
                    if (a != '%')
                        sb.append(a);
                    else
                    {
                        width = 0;
                        pwidth = 0;
                        alignRight = true;
                        leadZero = false;
                        state = PERCENT_STATE;
                    }
                    break;
                case PERCENT_STATE:
                    if (a == '%')
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    else if (a == '+')
                    {
                        alignRight = true;
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a == '-')
                    {
                        alignRight = false;
                        width = 0;
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a >= '0' && a <= '9')
                    {
                        if (a == '0')
                            leadZero = true;
                        alignRight = true;
                        width = (int) (a - '0');
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a == '.')
                    {
                        alignRight = true;
                        width = 0;
                        pwidth = 0;
                        state = POINT_DIGIT_STATE;
                    }
                    else if (a == 'c' || a == 'C')
                    {
                        obj = v.elementAt(pos++);
                        if (obj instanceof Character)
                            str = "" + (char) (((Character) obj).charValue());
                        else
                            str = "" + (char) (((Integer) obj).intValue());
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'x' || a == 'X')
                    {
                        obj = v.elementAt(pos++);
                        if (obj instanceof Character)
                            str = ""
                                    + Integer
                                            .toHexString((int) (((Character) obj)
                                                    .charValue()));
                        else
                            str = ""
                                    + Integer
                                            .toHexString((int) (((Integer) obj)
                                                    .intValue()));

                        if (a == 'X')
                            str = str.toUpperCase();
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'l' || a == 'L')
                    {
                        state = L_STATE;
                    }
                    else if (a == 'o' || a == 'O')
                    {
                        obj = v.elementAt(pos++);
                        if (obj instanceof Character)
                            str = ""
                                    + Integer
                                            .toOctalString((int) (((Character) obj)
                                                    .charValue()));
                        else
                            str = ""
                                    + Integer
                                            .toOctalString((int) (((Integer) obj)
                                                    .intValue()));
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'e' || a == 'E')
                    {
                        df = new DecimalFormat("##0.###E0");
                        str = df.format(((Double) v.elementAt(pos++))
                                .doubleValue());
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'd' || a == 'D' || a == 'u' || a == 'U')
                    {
                        str = "" + v.elementAt(pos++);
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 's' || a == 'f' || a == 'g' || a == 'S'
                            || a == 'F' || a == 'G')
                    {
                        str = "" + v.elementAt(pos++);
                        blen = byteLength(str); //
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - str.length()) + str);
                            else
                                sb.append(str + spaces(width - str.length()));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case L_STATE:
                    if (a == 'x' || a == 'X')
                    {
                        obj = v.elementAt(pos++);
                        if (obj instanceof Character)
                            str = ""
                                    + Long
                                            .toHexString((long) (((Character) obj)
                                                    .charValue()));
                        else if (obj instanceof Long)
                            str = ""
                                    + Long
                                            .toHexString(((Long) obj)
                                                    .longValue());
                        else
                            str = ""
                                    + Integer.toHexString(((Integer) obj)
                                            .intValue());

                        if (a == 'X')
                            str = str.toUpperCase();

                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - str.length()) + str);
                            else
                                sb.append(str + spaces(width - str.length()));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'd' || a == 'u' || a == 'D' || a == 'U')
                    {
                        str = "" + v.elementAt(pos++);
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case WIDTH_DIGIT_STATE:
                    if (a == '.')
                    {
                        pwidth = 0;
                        state = POINT_DIGIT_STATE;
                    }
                    else if (a >= '0' && a <= '9')
                    {
                        if (a == '0' && width == 0)
                            leadZero = true;
                        width = 10 * width + (int) (a - '0');
                    }
                    else if (a == 'c' || a == 'C')
                    {
                        obj = v.elementAt(pos++);
                        if (obj instanceof Character)
                            str = "" + (char) (((Character) obj).charValue());
                        else
                            str = "" + (char) (((Integer) obj).intValue());
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                        {
                            sb.append(str);
                        }
                        state = NORMAL_STATE;
                    }
                    else if (a == 'x' || a == 'X')
                    {
                        obj = v.elementAt(pos++);
                        if (obj instanceof Character)
                            str = ""
                                    + Integer
                                            .toHexString((int) (((Character) obj)
                                                    .charValue()));
                        else
                            str = ""
                                    + Integer
                                            .toHexString((int) (((Integer) obj)
                                                    .intValue()));

                        if (a == 'X')
                            str = str.toUpperCase();
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'o' || a == 'O')
                    {
                        obj = v.elementAt(pos++);
                        if (obj instanceof Character)
                            str = ""
                                    + Integer
                                            .toOctalString((int) (((Character) obj)
                                                    .charValue()));
                        else
                            str = ""
                                    + Integer
                                            .toOctalString((int) (((Integer) obj)
                                                    .intValue()));

                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'e' || a == 'E')
                    {
                        df = new DecimalFormat("##0.###E0");
                        str = df.format(((Double) v.elementAt(pos++))
                                .doubleValue());
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'l' || a == 'L')
                    {
                        state = L_STATE;
                    }
                    else if (a == 'd' || a == 'D' || a == 'u' || a == 'U')
                    {
                        // str = "" + leadZero + " " + alignRight + v.elementAt(pos++);
                        str = "" + v.elementAt(pos++);
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 's' || a == 'f' || a == 'g' || a == 'S'
                            || a == 'F' || a == 'G')
                    {
                        str = "" + v.elementAt(pos++);
                        blen = byteLength(str);
                        if (width > str.length())
                        {
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case POINT_DIGIT_STATE:
                    if (a >= '0' && a <= '9')
                    {
                        pwidth = 10 * pwidth + (int) (a - '0');
                    }
                    else if (a == 'c' || a == 'C')
                    {
                        obj = v.elementAt(pos++);
                        if (obj instanceof Character)
                            str = "" + (char) (((Character) obj).charValue());
                        else
                            str = "" + (char) (((Integer) obj).intValue());
                        blen = byteLength(str);
                        if (width > blen)
                        {
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        state = NORMAL_STATE;
                    }
                    else if (a == 'l' || a == 'L')
                    {
                        state = L_STATE;
                    }
                    else if (a == 'd' || a == 'D' || a == 'u' || a == 'U')
                    {
                        str = "" + v.elementAt(pos++);
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 's' || a == 'S')
                    {
                        str = "" + v.elementAt(pos++);
                        blen = byteLength(str);
                        if (width > blen)
                        {
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'e' || a == 'E')
                    {
                        df = new DecimalFormat("##0.###E0");
                        str = df.format(((Double) v.elementAt(pos++))
                                .doubleValue());
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'f' || a == 'g' || a == 'F' || a == 'G')
                    {
                        if (pwidth > 0)
                        {
                            df = new DecimalFormat("0." + zeros(pwidth));
                            str = df.format(((Double) v.elementAt(pos++))
                                    .doubleValue());
                            blen = byteLength(str);
                            if (width >= blen)
                            { //
                                if (alignRight)
                                    sb.append(spaces(width - blen) + str);
                                else
                                    sb.append(str + spaces(width - blen));
                            }
                        }
                        else
                        {
                            str = "" + v.elementAt(pos++);
                            blen = byteLength(str);
                            if (width > blen)
                            {
                                if (alignRight)
                                    sb.append(spaces(width - blen) + str);
                                else
                                    sb.append(str + spaces(width - blen));
                            }
                        }
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
            }
        }
        return sb.toString();
    }

    @SuppressWarnings("rawtypes")
    public static String format(String fmt, ArrayList arr)
    {
        StringBuffer sb = new StringBuffer();
        char[] chars = new char[fmt.length()];
        fmt.getChars(0, fmt.length(), chars, 0);
        final int NORMAL_STATE = 0;
        final int PERCENT_STATE = 1;
        final int L_STATE = 2;
        final int WIDTH_DIGIT_STATE = 3;
        final int POINT_DIGIT_STATE = 4;
        int state = NORMAL_STATE;
        int width = 0;
        int pwidth = 0;
        int blen = 0;
        boolean alignRight = true;
        int pos = 0;
        DecimalFormat df = null;
        Object obj = null;
        String str = "";
        boolean leadZero = false;
        char a;
        for (int i = 0; i < chars.length; i++)
        {
            a = chars[i];
            switch (state)
            {
                case NORMAL_STATE:
                    if (a != '%')
                        sb.append(a);
                    else
                    {
                        width = 0;
                        pwidth = 0;
                        alignRight = true;
                        leadZero = false;
                        state = PERCENT_STATE;
                    }
                    break;
                case PERCENT_STATE:
                    if (a == '%')
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    else if (a == '+')
                    {
                        alignRight = true;
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a == '-')
                    {
                        alignRight = false;
                        width = 0;
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a >= '0' && a <= '9')
                    {
                        if (a == '0')
                            leadZero = true;
                        alignRight = true;
                        width = (int) (a - '0');
                        state = WIDTH_DIGIT_STATE;
                    }
                    else if (a == '.')
                    {
                        alignRight = true;
                        width = 0;
                        pwidth = 0;
                        state = POINT_DIGIT_STATE;
                    }
                    else if (a == 'c' || a == 'C')
                    {
                        obj = arr.get(pos++);
                        if (obj instanceof Character)
                            str = "" + (char) (((Character) obj).charValue());
                        else
                            str = "" + (char) (((Integer) obj).intValue());
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'x' || a == 'X')
                    {
                        obj = arr.get(pos++);
                        if (obj instanceof Character)
                            str = ""
                                    + Integer
                                            .toHexString((int) (((Character) obj)
                                                    .charValue()));
                        else
                            str = ""
                                    + Integer
                                            .toHexString((int) (((Integer) obj)
                                                    .intValue()));

                        if (a == 'X')
                            str = str.toUpperCase();
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'l' || a == 'L')
                    {
                        state = L_STATE;
                    }
                    else if (a == 'o' || a == 'O')
                    {
                        obj = arr.get(pos++);
                        if (obj instanceof Character)
                            str = ""
                                    + Integer
                                            .toOctalString((int) (((Character) obj)
                                                    .charValue()));
                        else
                            str = ""
                                    + Integer
                                            .toOctalString((int) (((Integer) obj)
                                                    .intValue()));
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'e' || a == 'E')
                    {
                        df = new DecimalFormat("##0.###E0");
                        str = df
                                .format(((Double) arr.get(pos++)).doubleValue());
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'd' || a == 'D' || a == 'u' || a == 'U')
                    {
                        str = "" + arr.get(pos++);
                        sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 's' || a == 'f' || a == 'g' || a == 'S'
                            || a == 'F' || a == 'G')
                    {
                        str = "" + arr.get(pos++);
                        blen = byteLength(str); //
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - str.length()) + str);
                            else
                                sb.append(str + spaces(width - str.length()));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case L_STATE:
                    if (a == 'x' || a == 'X')
                    {
                        obj = arr.get(pos++);
                        if (obj instanceof Character)
                            str = ""
                                    + Long
                                            .toHexString((long) (((Character) obj)
                                                    .charValue()));
                        else if (obj instanceof Long)
                            str = ""
                                    + Long
                                            .toHexString(((Long) obj)
                                                    .longValue());
                        else
                            str = ""
                                    + Integer.toHexString(((Integer) obj)
                                            .intValue());

                        if (a == 'X')
                            str = str.toUpperCase();

                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - str.length()) + str);
                            else
                                sb.append(str + spaces(width - str.length()));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'd' || a == 'u' || a == 'D' || a == 'U')
                    {
                        str = "" + arr.get(pos++);
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case WIDTH_DIGIT_STATE:
                    if (a == '.')
                    {
                        pwidth = 0;
                        state = POINT_DIGIT_STATE;
                    }
                    else if (a >= '0' && a <= '9')
                    {
                        if (a == '0' && width == 0)
                            leadZero = true;
                        width = 10 * width + (int) (a - '0');
                    }
                    else if (a == 'c' || a == 'C')
                    {
                        obj = arr.get(pos++);
                        if (obj instanceof Character)
                            str = "" + (char) (((Character) obj).charValue());
                        else
                            str = "" + (char) (((Integer) obj).intValue());
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                        {
                            sb.append(str);
                        }
                        state = NORMAL_STATE;
                    }
                    else if (a == 'x' || a == 'X')
                    {
                        obj = arr.get(pos++);
                        if (obj instanceof Character)
                            str = ""
                                    + Integer
                                            .toHexString((int) (((Character) obj)
                                                    .charValue()));
                        else
                            str = ""
                                    + Integer
                                            .toHexString((int) (((Integer) obj)
                                                    .intValue()));

                        if (a == 'X')
                            str = str.toUpperCase();
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'o' || a == 'O')
                    {
                        obj = arr.get(pos++);
                        if (obj instanceof Character)
                            str = ""
                                    + Integer
                                            .toOctalString((int) (((Character) obj)
                                                    .charValue()));
                        else
                            str = ""
                                    + Integer
                                            .toOctalString((int) (((Integer) obj)
                                                    .intValue()));

                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'e' || a == 'E')
                    {
                        df = new DecimalFormat("##0.###E0");
                        str = df
                                .format(((Double) arr.get(pos++)).doubleValue());
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'l' || a == 'L')
                    {
                        state = L_STATE;
                    }
                    else if (a == 'd' || a == 'D' || a == 'u' || a == 'U')
                    {
                        // str = "" + leadZero + " " + alignRight + arr.get(pos++);
                        str = "" + arr.get(pos++);
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 's' || a == 'f' || a == 'g' || a == 'S'
                            || a == 'F' || a == 'G')
                    {
                        str = "" + arr.get(pos++);
                        blen = byteLength(str);
                        if (width > str.length())
                        {
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
                case POINT_DIGIT_STATE:
                    if (a >= '0' && a <= '9')
                    {
                        pwidth = 10 * pwidth + (int) (a - '0');
                    }
                    else if (a == 'c' || a == 'C')
                    {
                        obj = arr.get(pos++);
                        if (obj instanceof Character)
                            str = "" + (char) (((Character) obj).charValue());
                        else
                            str = "" + (char) (((Integer) obj).intValue());
                        blen = byteLength(str);
                        if (width > blen)
                        {
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        state = NORMAL_STATE;
                    }
                    else if (a == 'l' || a == 'L')
                    {
                        state = L_STATE;
                    }
                    else if (a == 'd' || a == 'D' || a == 'u' || a == 'U')
                    {
                        str = "" + arr.get(pos++);
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (leadZero && alignRight)
                                sb.append(zeros(width - blen) + str);
                            else if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 's' || a == 'S')
                    {
                        str = "" + arr.get(pos++);
                        blen = byteLength(str);
                        if (width > blen)
                        {
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'e' || a == 'E')
                    {
                        df = new DecimalFormat("##0.###E0");
                        str = df
                                .format(((Double) arr.get(pos++)).doubleValue());
                        blen = byteLength(str);
                        if (width >= blen)
                        { //
                            if (alignRight)
                                sb.append(spaces(width - blen) + str);
                            else
                                sb.append(str + spaces(width - blen));
                        }
                        else
                            sb.append(str);
                        state = NORMAL_STATE;
                    }
                    else if (a == 'f' || a == 'g' || a == 'F' || a == 'G')
                    {
                        if (pwidth > 0)
                        {
                            df = new DecimalFormat("0." + zeros(pwidth));
                            str = df.format(((Double) arr.get(pos++))
                                    .doubleValue());
                            blen = byteLength(str);
                            if (width >= blen)
                            { //
                                if (alignRight)
                                    sb.append(spaces(width - blen) + str);
                                else
                                    sb.append(str + spaces(width - blen));
                            }
                        }
                        else
                        {
                            str = "" + arr.get(pos++);
                            blen = byteLength(str);
                            if (width > blen)
                            {
                                if (alignRight)
                                    sb.append(spaces(width - blen) + str);
                                else
                                    sb.append(str + spaces(width - blen));
                            }
                        }
                        state = NORMAL_STATE;
                    }
                    else
                    {
                        sb.append(a);
                        state = NORMAL_STATE;
                    }
                    break;
            }
        }
        return sb.toString();
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args)
    {
        Vector vect = new Vector();
        vect.add("안녕?");
        vect.add(new Integer(2030));
        vect.add(new Double(Math.PI));
        vect.add(new Character('A'));
        vect.add(new Integer(97));
        String str;
        str = format("|%s| Java!!", vect);
        System.out.println(str);

        str = format("|%10s| Java!", vect);
        System.out.println(str);

        str = format("%-10s Java!", vect);
        System.out.println(str);

        str = format("%-10s %%Java!", vect);
        System.out.println(str);

        str = format("%-10s: %5d%%Java!", vect);
        System.out.println(str);

        str = format("%-10s: %-8d%% Java!", vect);
        System.out.println(str);

        str = format("%-10s: %-8d%% Java!  pi = %g", vect);
        System.out.println(str);

        str = format("%-10s: %-8d%% Java!  pi = %12.3g", vect);
        System.out.println(str);

        str = format("%-10s: %-8d%% Java!  pi = %-10.4g", vect);
        System.out.println(str);

        str = format("%-10s: %-8d%% Java!  pi = %-10.4g %c%-3c", vect);
        System.out.println(str);

        str = format("%-10s: %-8d%% Java!  pi = %e %c%-3c", vect);
        System.out.println(str);

        str = format("%-10s: %X%% Java!  pi = %e %c%-3c", vect);
        System.out.println(str);

        str = format("%-10s: %6X%% Java!  pi = %e %c%-3c", vect);
        System.out.println(str);

        str = format("%-10s: %-6X%% Java!  pi = %e %c%-3c", vect);
        System.out.println(str);

        str = format("%-10s: %O%% Java!  pi = %e %c%-3c", vect);
        System.out.println(str);

        str = format("%-10s: %8o%% Java!  pi = %e %c%-3c", vect);
        System.out.println(str);

        str = format("%-10s: %-8O%% Java!  pi = %e %c%-3c", vect);
        System.out.println(str);

        str = format("%-10s: %10ld%% Java!  pi = %e %c%-3c", vect);
        System.out.println(str);

        System.out.println(Sprintf.format(" %7s :", true));
        System.out.println(Sprintf.format(" %7s :", false));
        System.out.println(Sprintf.format(" %7s :", true, "Maleq", "Female"));
        System.out.println(Sprintf.format(" %7s :", false, "Male", "Femaleq"));
        System.out.println(Sprintf.format(" %7c :", 65));
        System.out.println(Sprintf.format(" %-7c :", 97));
        System.out.println(Sprintf.format(" %7c :", 0xAC00));
        System.out.println(Sprintf.format(" %7s :%% ", "가나다"));
        System.out.println(Sprintf.format(" %+07X :%% ", 123));
        System.out.println(Sprintf.format("(%7s):%% ", new StringBuffer("가나")));
    }
}

